﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SocialLearning
{
    public class UAC_Account : I_UAC_Account, I_UAC_Authentication
    {
        #region Private Fields

        public int uid { get; set; }
        //private UAC_Account_Control uaControl = new UAC_Account_Control();
        private I_UAC_BasicDetails basicAccountDetails;
        private I_UAC_AdvancedDetails advancedAccountDetails;
        private I_UAC_MarketingDetails marketingDetails;
        private I_UAC_EducationalBackground educationBackground;
        private string accountUID;
        private string password;
        private string secretQuestion;
        private string secretPassCode;
        private bool status;

        #endregion

        #region Public Fields

        public static List<UAC_Account> accountlist = new List<UAC_Account>();

        #endregion

        #region Properties

        public I_UAC_BasicDetails BasicAccountDetails
        {
            get
            {
                return basicAccountDetails;
            }
            set
            {
                basicAccountDetails = value;
            }
        }

        public I_UAC_AdvancedDetails AdvancedAccountDetails
        {
            get
            {
                return advancedAccountDetails;
            }
            set
            {
                advancedAccountDetails = value;
            }
        }

        public I_UAC_MarketingDetails MarketingDetails
        {
            get
            {
                return marketingDetails;
            }
            set
            {
                marketingDetails = value;
            }
        }

        public I_UAC_EducationalBackground EducationBackground
        {
            get
            {
                return educationBackground;
            }
            set
            {
                educationBackground = value;
            }
        }

        public string AccountUID
        {
            get
            {
                return accountUID;
            }
            set
            {
                accountUID = value;
            }
        }

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        public string SecretQuestion
        {
            get
            {
                return secretQuestion;
            }
            set
            {
                secretQuestion = value;
            }
        }

        public string SecretPassCode
        {
            get
            {
                return secretPassCode;
            }
            set
            {
                secretPassCode = value;
            }
        }

        public bool Status
        {
            get
            {
                return status;
            }
            set
            {
                status = value;
            }
        }
        #endregion

        #region Public Methods

        /// <summary>
        /// Determines whether the specified email is registered.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <returns>
        ///   <c>true</c> if the specified email is registered; otherwise, <c>false</c>.
        /// </returns>
        public bool IsRegistered(string email)
        {
            foreach (UAC_Account compUser in UAC_UserManagement.userlist)
            {
                if (compUser.BasicAccountDetails.Email == email)
                {
                    if (compUser.Status)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Resets the password.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="secretcdoe">The secretcdoe.</param>
        /// <returns></returns>
        public bool ResetPassword(string email, string secretcdoe)
        {
            if (IsRegistered(email))
            {
                foreach (UAC_Account compUser in UAC_UserManagement.userlist)
                {
                    if (compUser.BasicAccountDetails.Email == email)
                    {
                        if (compUser.secretPassCode == secretPassCode)
                        {
                            compUser.password = secretcdoe;
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        #endregion

        #region Engine Implementation
        public string SerializeObject()
        {
            return string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9}", uid, basicAccountDetails.uid, advancedAccountDetails.uid, marketingDetails.uid, educationBackground.uid, accountUID, password, secretQuestion, secretPassCode, status);
        }

        public bool GenerateObject(string ss)
        {
            bool _return = false;

            List<string> content = ss.Split(';').ToList();
            uid = Convert.ToInt32(content[0]);
            foreach (UAC_BasicDetails uac_bd in UAC_Engine.SL_UAC_BasicDetails)
            {
                if (uac_bd.uid == Convert.ToInt32(content[1]))
                {
                    basicAccountDetails = uac_bd;
                }
            }
            foreach (UAC_AdvancedDetails uac_ad in UAC_Engine.SL_UAC_AdvancedDetails)
            {
                if (uac_ad.uid == Convert.ToInt32(content[2]))
                {
                    advancedAccountDetails = uac_ad;
                }
            }
            foreach (UAC_MarketingDetails uac_md in UAC_Engine.SL_UAC_MarketingDetails)
            {
                if (uac_md.uid == Convert.ToInt32(content[3]))
                {
                    marketingDetails = uac_md;
                }
            }
            foreach (UAC_EducationalBackground uac_ed in UAC_Engine.SL_UAC_EducationalBackground)
            {
                if (uac_ed.uid == Convert.ToInt32(content[4]))
                {
                    educationBackground = uac_ed;
                }
            }
            accountUID = content[5];
            password = content[6];
            secretQuestion = content[7];
            secretPassCode = content[8];
            status = Convert.ToBoolean(content[9]);

            return _return;
        }
        #endregion

        #region UAC_Account_Control Implementation

        //// Get Single Account
        //public void GetAccountSingle(int id)
        //{
        //    var tempAccount = uaControl.GetUAC_Account(id);
        //}

        //// Get Account List
        //public void GetAccountList()
        //{
        //    accountlist = uaControl.GetUAC_Accounts();
        //}

        //// Update or Save Account
        //public void UpdateSave(UAC_Account account)
        //{
        //    uaControl.SaveOrUpdateUAC_Account(account);
        //}

        //// Delete Account
        //public void Delete(UAC_Account account)
        //{
        //    uaControl.DeleteUAC_Account(account);
        //}

        #endregion
    }
}
